﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;


namespace RemoteDesktops.Classes
{
  /// <summary>
  /// 
  /// </summary>
  public class Desktops : List<Desktop>
  {
    /// <summary>
    /// 
    /// </summary>
    private const string fileName = "Desktops.xml";
    /// <summary>
    /// 
    /// </summary>
    private XDocument _xDoc;
    /// <summary>
    /// 
    /// </summary>
    private string _filePath;

    /// <summary>
    /// 
    /// </summary>
    private void AddSave(Desktop desktop)
    {
      try
      { 
        this._xDoc.Root.Add(new XElement("Desktop",
                             new XAttribute("ServerName", desktop.ServerName),
                             new XAttribute("Description", desktop.Description),
                             new XAttribute("Alias", desktop.Alias),
                             new XAttribute("Id", desktop.Id)
                            ));
        this._xDoc.Save(this._filePath);        
      }
      catch
      {
        throw;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="desktop"></param>
    private void RemoveSave(Desktop desktop)
    {
      XElement xel = this._xDoc.Root.Elements("Desktop").Where(ex => new Guid(ex.Attribute("Id").Value).Equals(desktop.Id)).FirstOrDefault();
      if (xel != null)
        xel.Remove();
      this._xDoc.Save(this._filePath);
    }

    /// <summary>
    /// 
    /// </summary>
    public Desktops()
    {
      this._filePath = String.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, fileName);
      LoadDesktops();
    }

    /// <summary>
    /// 
    /// </summary>
    public void LoadDesktops()
    {
      try
      {
        this._xDoc = XDocument.Load(this._filePath);
        foreach (XElement elem in  this._xDoc.Root.Elements("Desktop"))
        {
          this.Add(new Desktop(elem.Attribute("Alias").Value, elem.Attribute("ServerName").Value,  
                               elem.Attribute("Description").Value, new Guid(elem.Attribute("Id").Value)));                    
        }
      }
      catch
      {
        throw;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="desktop"></param>
    public void AddDesktop(Desktop desktop)
    {
      this.Add(desktop);      
      this.AddSave(desktop);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="desktop"></param>
    public void RemoveDesktop(Desktop desktop)
    {
      this.Remove(desktop);      
      this.RemoveSave(desktop);
    }

    /// <summary>
    /// Finds a Desktop
    /// </summary>
    /// <param name="alias"></param>
    /// <returns></returns>
    public Desktop Find(string alias)
    {
      return this.Find
      (
        delegate(Desktop desktop)
        {
          return desktop.Alias == alias;
        }
      );
    }

    /// <summary>
    /// Finds a Desktop by Id
    /// </summary>
    /// <param name="alias"></param>
    /// <returns></returns>
    public Desktop Find(Guid id)
    {
      return this.Find
      (
        delegate(Desktop desktop)
        {
          return desktop.Id == id;
        }
      );
    }
  }

  /// <summary>
  /// 
  /// </summary>
  public class Desktop
  {
    /// <summary>
    /// 
    /// </summary>
    public Desktop()
    {

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="alias"></param>
    /// <param name="serverName"></param>
    public Desktop(String alias, String serverName, String desc, Guid id)
    {
      this.Alias = alias;
      this.ServerName = serverName;
      this.Description = desc;
      this.Id = id;
    }

    /// <summary>
    /// 
    /// </summary>
    public String Alias
    {
      get;
      set; 
    }

    /// <summary>
    /// 
    /// </summary>
    public String ServerName
    {
      get;
      set;
    }

    /// <summary>
    /// 
    /// </summary>
    public String Description
    {
      get;
      set;
    }

    /// <summary>
    /// 
    /// </summary>
    public Guid Id
    {
      get;
      set;
    }
  }
}
